// Require the module
var FitParser = require("./node_modules/fit-file-parser").default;

// 文档参考：https://www.npmjs.com/package/fit-file-parser

// const { debug } = require("console");
// Read a .FIT file
var fs = require("fs");
var fsForSave = require("fs");
var fitDataJSONString = '["NO DATA"]';
fs.readFile(
    // 要解析的fit文件路径
    "./YourFitFile.fit",
    function (err, content) {
        // Create a FitParser instance (options argument is optional)
        var fitParser = new FitParser({
            force: true,
            speedUnit: "km/h",
            lengthUnit: "m",
            temperatureUnit: "celsius",
            pressureUnit: "bar", // accept bar, cbar and psi (default is bar)
            elapsedRecordField: true,
            mode: "both", //"cascade, list, both",
        });
        // Parse your file
        fitParser.parse(content, function (error, data) {
            // Handle result of parse method
            if (error) {
                console.log("❌Parsing error!");
                console.log(error);
            } else {
                // console.log(JSON.stringify(data));
                fitDataJSONString = JSON.stringify(data, "", "\t");
                fsForSave.writeFileSync("./data.json", fitDataJSONString);
                console.log("✅Parsing completed!");
            }
        });
    }
);
